var express = require('express');
var router = express.Router();

require("../db/mongoose-user")
var User = require("../db/mongoose-user").User;

//连接字符串
var DB_CONN_STR = 'mongodb://localhost/notes';


router.post('/login', function (req, res, next) {
    var json =
        {
            "code": "1",
            "succeed": "succeed",
            "content": {
                "msg": "密码错误"
            }
        };
    var platform = req.param('platform');
    var user_name = req.param('user_name');
    var user_password = req.param('user_password');

    User.find({'user_name': user_name}, function (err, docs) {
        // docs 是查询的结果数组 });
        if (err) {
            console.log(err);
        } else if (docs.length == 0) {
            FailureJson(json, res, "用户不存在");
        } else {
            var item=docs[0];
            if (item.user_password == user_password)
                SucceeJson(json, res, "登录成功");
            else
                FailureJson(json, res, "密码错误");

        }

    });


});
var FailureJson = function (json, res, msg) {
    json.code = "0";
    json.succeed = "failure";
    json.content.msg = msg;
    res.send(json);
};
var SucceeJson = function (json, res, msg) {
    json.code = "1";
    json.succeed = "succeed";
    json.content.msg = msg;
    res.send(json);
};

module.exports = router;
